package com.hangzhou.xyj.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.UserDao;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.event.CorrectRespEvent;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.manager.ScreenManager;
import com.hangzhou.xyj.model.DramaCategoryModel;
import com.hangzhou.xyj.model.type.ECorrectType;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.UpdateHobbyHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.listener.HttpResponseListener;
import com.hangzhou.xyj.net.request.UpdateHobbyReq;
import com.hangzhou.xyj.ui.activity.ExpertListActivity;
import com.hangzhou.xyj.ui.activity.MoreShowActivity;
import com.hangzhou.xyj.ui.activity.SingingListActivity;
import com.hangzhou.xyj.ui.adapter.base.AbsBaseAdapter;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

/**
 * Created by DT-038 on 2016/1/6.
 * 更多剧种adapter
 */
public class DramaCategoryListAdapter extends AbsBaseAdapter<DramaCategoryModel> {

    private int mIntentTag;

    public DramaCategoryListAdapter(Context context, List<DramaCategoryModel> data) {
        super(context, data);
    }

    public void setIntentTag(int intentTag) {
        this.mIntentTag = intentTag;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType) {
            default:
                viewHolder = new MoreDramaViewHolder(mLayoutInflater.inflate(R.layout.layout_more_drama_item, viewGroup, false));
                break;
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder == null) {
            return;
        }
        if (viewHolder instanceof MoreDramaViewHolder) {
            ((MoreDramaViewHolder) viewHolder).setData(mDataList.get(position));
        }
    }

    class MoreDramaViewHolder extends RecyclerView.ViewHolder {
        private MyTextView mTvDrama;//剧种名称
        private View mTopPadding;//上面的黑块
        private View mBotttomPadding;//下面的黑块
        private View mUnderLine;//下划线

        public MoreDramaViewHolder(View itemView) {
            super(itemView);
            initView(itemView);

        }

        private void initView(View itemView) {
            mTvDrama = (MyTextView) itemView.findViewById(R.id.tv_drama_item);
            mTopPadding = itemView.findViewById(R.id.v_top_padding);
            mBotttomPadding = itemView.findViewById(R.id.v_bottom_padding);
            mUnderLine = itemView.findViewById(R.id.v_underline);
        }

        public void setData(final DramaCategoryModel model) {
            if (model.isLastOne() && model.getIsAreaFirst()) {
                mBotttomPadding.setVisibility(View.VISIBLE);
                mUnderLine.setVisibility(View.GONE);
                mTopPadding.setVisibility(View.VISIBLE);
            } else if (model.isLastOne()) {
                mBotttomPadding.setVisibility(View.VISIBLE);
                mUnderLine.setVisibility(View.GONE);
            } else if (model.getIsAreaFirst() && model.isAreaLast()) {
                mTopPadding.setVisibility(View.VISIBLE);
                mUnderLine.setVisibility(View.GONE);
            } else if (model.getIsAreaFirst()) {
                mTopPadding.setVisibility(View.VISIBLE);
            } else if (model.isAreaLast()) {
                mUnderLine.setVisibility(View.GONE);
            } else {
            }
            mTvDrama.setText(model.getName());
            itemView.setOnClickListener(new View.OnClickListener() {
                Intent intent;

                @Override
                public void onClick(View view) {
                    switch (EIntentTagType.valueOf(mIntentTag)) {
                        case TAG_DIAN_DRAMA_CATEGORY://点戏-点戏台-分类点戏
                            intent = new Intent(mContext, SingingListActivity.class);
                            intent.putExtra(DataKey.INTENT_TAG, mIntentTag);
                            intent.putExtra(DataKey.TITLE, model.getName());
                            intent.putExtra(DataKey.INDEX, String.valueOf(model.getId()));
                            IntentManager.getInstance().goActivity(mContext, intent);
                            break;
                        case TAG_STAGE_EXPERT://戏台-名家 进入
                            intent = new Intent(mContext, ExpertListActivity.class);
                            intent.putExtra(DataKey.INTENT_TAG, mIntentTag);
                            intent.putExtra(DataKey.INDEX, model.getId());
                            intent.putExtra(DataKey.TITLE, model.getName() + "-名家");
                            IntentManager.getInstance().goActivity(mContext, intent);
                            break;
                        case TAG_STAGE_TEAM://戏台-名团 进入
                            intent = new Intent(mContext, ExpertListActivity.class);
                            intent.putExtra(DataKey.INDEX, model.getId());
                            intent.putExtra(DataKey.INTENT_TAG, mIntentTag);
                            intent.putExtra(DataKey.TITLE, model.getName() + "-演员");
                            IntentManager.getInstance().goActivity(mContext, intent);
                            break;
                        case TAG_MINE_PERSONAL_INFO://我-个人信息 进入
                            updateHobby(model.getName());
                            break;
                        case TAG_ADD_GROUP://新建圈子-进入
                            CorrectRespEvent event = new CorrectRespEvent();
                            event.setValue(model.getName());
                            event.setValue2(String.valueOf(model.getId()));
                            event.setTag(ECorrectType.CHOOSE.getValue());
                            EventBusUtil.postEvent(event);
                            ScreenManager.getInstance().currentActivity().finish();
                            break;
                        case TAG_FIND_SHOW://发现-演出  进入
                            intent = new Intent(mContext, MoreShowActivity.class);
                            intent.putExtra(DataKey.INDEX, model.getId());
                            intent.putExtra(DataKey.TITLE, model.getName());
                            IntentManager.getInstance().goActivity(mContext, intent);
                            break;
                        case TAG_DRAMA_GROUP://发现-剧团
                            intent = new Intent(mContext, ExpertListActivity.class);
                            intent.putExtra(DataKey.INTENT_TAG, mIntentTag);
                            intent.putExtra(DataKey.INDEX, model.getId());
                            intent.putExtra(DataKey.TITLE, model.getName() + "-剧团");
                            IntentManager.getInstance().goActivity(mContext, intent);
                            break;
                    }
                }
            });
        }

        public void updateHobby(final String hobby) {
            UpdateHobbyReq updateHobbyReq = new UpdateHobbyReq(Integer.valueOf(CommonPreferences.getUserId(mContext)), hobby);
            HttpLoader.getInstance().sendRequest(new UpdateHobbyHandler(updateHobbyReq, new HttpResponseListener() {
                @Override
                public void onStart() {

                }

                @Override
                public void onSuccess(Object model) {
                    if ((Boolean) model) {
                        //更新应用中的数据
                        XYJApplication.getUserInfo().setHobby(hobby);
                        //更新本地数据库中的信息
                        UserDao dao = new UserDao(mContext);
                        dao.addUser(XYJApplication.getUserInfo());
                        ToastUtil.show("更新爱好成功");
                        CorrectRespEvent event = new CorrectRespEvent();
                        event.setValue(hobby);
                        event.setTag(ECorrectType.HOBBY.getValue());
                        EventBusUtil.postEvent(event);
                        ScreenManager.getInstance().currentActivity().finish();
                    }
                }

                @Override
                public void onFail(String msg) {

                }

                @Override
                public void onDone(String msg) {

                }

                @Override
                public void onError() {

                }
            }, 0));
        }
    }
}
